<template>
  <div class="skill-certificate-list">
    <div class="breadcrumb">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item to="Home" :replace="true">首页</el-breadcrumb-item>
        <el-breadcrumb-item>技能证书列表</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <el-form :inline="true" :model="searchParams">
      <el-form-item label="应聘者姓名" prop="candidateName" v-if="findCandidateFormItem">
        <el-input v-model="searchParams.candidateName" placeholder="请选择应聘者姓名" readonly="readonly">
          <i slot="suffix" class="el-input__icon el-icon-search" @click="findCandidate"></i>
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSearch" v-if="findCandidateFormItem">查询</el-button>
        <el-button @click="onReset" v-if="findCandidateFormItem">重置</el-button>
        <el-button type="primary" @click="onAdd">新增</el-button>
        <el-button type="primary" @click="onExport">导出</el-button>
      </el-form-item>
    </el-form>

    <el-table
      :data="pageData.list"
      border
      v-loading="pageData.loading"
      size="mini"
      header-cell-class-name="table-header-cell-font"
      cell-class-name="table-cell-font">
      <el-table-column prop="candidateName" label="应聘者姓名"></el-table-column>
      <el-table-column prop="certificateName" label="证书名称"></el-table-column>
      <el-table-column prop="issuingAuthority" label="颁发机构"></el-table-column>
      <el-table-column prop="issueDate" label="颁发时间"></el-table-column>
      <el-table-column prop="expiryDate" label="到期日期"></el-table-column>
      <el-table-column fixed="right" label="操作" width="250">
        <template v-slot="{ row }">
          <el-button @click.native.prevent="editRow(row.id)" type="primary">编辑</el-button>
          <el-button @click.native.prevent="delRow(row.id)" type="danger" plain>删除</el-button>
          <el-button @click.native.prevent="detailRow(row.id)" type="primary" plain>详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="table-pagination">
      <el-pagination
        @current-change="paginationChange"
        :current-page="pageData.current"
        :page-sizes="pageData.pageSizes"
        :page-size="pageData.size"
        background
        layout="total, prev, pager, next, jumper"
        :total="pageData.count">
      </el-pagination>
    </div>
    <el-dialog :visible.sync="candidateSelectorVisible" v-if="candidateSelectorVisible" title="应聘者基本信息选择器(双击行选中)" :top="dialogTop" :width="dialogWidth" :modal-append-to-body="true">
      <candidate-selector @candidate-selected-event="handleCandidateSelectedEvent">
      </candidate-selector>
    </el-dialog>
    <el-dialog :visible.sync="skillCertificateAddVisible" v-if="skillCertificateAddVisible" :title="skillCertificateAddTitle" :top="dialogTop" :width="dialogWidth" :modal-append-to-body="true">
      <skill-certificate-add :id="selectedSkillCertificateId" @close-skill-certificate-add-event="handleCloseSkillCertificateAddEvent" @reset-skill-certificate-add-event="handleResetSkillCertificateAddEvent">
      </skill-certificate-add>
    </el-dialog>
    <el-dialog :visible.sync="skillCertificateViewVisible" v-if="skillCertificateViewVisible" title="技能证书详情" :top="dialogTop" :width="dialogWidth">
      <skill-certificate-view :id="selectedSkillCertificateId" @close-skill-certificate-view-event="handleCloseSkillCertificateViewEvent">
      </skill-certificate-view>
    </el-dialog>
  </div>
</template>

<script>
import skillCertificateApi from '@/api/skillCertificateApi'
import CandidateSelector from "@/views/candidate/CandidateSelector.vue";
import SkillCertificateAdd from "@/views/skillCertificate/SkillCertificateAdd.vue"
import SkillCertificateView from "@/views/skillCertificate/SkillCertificateView.vue"
import listQueryMixin from '@/mixins/listQueryMixin'

export default {
  name: 'SkillCertificateList',
  components: {CandidateSelector,SkillCertificateAdd, SkillCertificateView},
  mixins: [
    listQueryMixin
  ],
  data () {
    return {
      searchParams: {
        candidateId: '',
        candidateName: '',
      },
      candidateSelectorVisible: false,
      skillCertificateAddVisible: false,
      skillCertificateAddTitle: '',
      skillCertificateViewVisible: false,
      selectedSkillCertificateId: 0,
      selectedEducationId: 0,
      findCandidateFormItem: true
    }
  },
  created () {
    const type = sessionStorage.getItem('type')
    if (type && Number(type) === 3) {
      this.searchParams.candidateId = 2
      this.findCandidateFormItem = false
    }
  },
  methods: {
    getPageData (p) {
      return skillCertificateApi.find(p)
    },
    onSearch () {
      this._getPageData()
    },
    onReset () {
      this.searchParams = {
        candidateId: '',
        candidateName: '',
      }
      this.pageData.pageIndex = 1
      this._getPageData()
    },
    onAdd () {
      this.selectedSkillCertificateId = ''
      this.skillCertificateAddVisible = true
      this.skillCertificateAddTitle = '技能证书新增'
    },
    onExport () {
      const headers = ['应聘者姓名','证书名称','颁发机构','颁发时间','到期日期']
      const params = Object.assign(this.getPaginationParams(), this.searchParams)
      this.getPageData(params).then(data => {
        if (!data || !data.data || data.data.list.length < 1) {
          this.$message.error('无数据导出')
          return
        }
        const exportData = []
        for (const d of data.data.list) {
          exportData.push([d.candidateName,d.certificateName,d.issuingAuthority,d.issueDate,d.expiryDate])
        }
        this.exportToExcel(headers, exportData)
      })
    },
    editRow (id) {
      this.selectedSkillCertificateId = id
      this.skillCertificateAddVisible = true
      this.skillCertificateAddTitle = '技能证书编辑'
    },
    delRow (id) {
      if (!id) {
        return
      }
      this.$confirm('确定要删除吗?', '删除提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        skillCertificateApi.del(id)
          .then((resp) => {
            if (resp && resp.code === 1) {
              this.$message.success('删除成功!')
              this.onSearch()
            } else {
              this.$message.error('删除失败:' + (resp.msg ? resp.msg : '原因未知'))
            }
          })
          .catch(error => {
            console.error('操作失败:', error)
          })
      }).catch(() => {})
    },
    detailRow (id) {
      this.selectedSkillCertificateId = id
      this.skillCertificateViewVisible = true
    },
    findCandidate () {
      this.candidateSelectorVisible = true
    },
    handleCandidateSelectedEvent (selectedCandidate) {
      this.candidateSelectorVisible = false
      if (selectedCandidate && 'candidateId' in selectedCandidate && this.searchParams.candidateId !== selectedCandidate['candidateId']) {
        this.searchParams.candidateId = selectedCandidate['candidateId']
        this.searchParams.candidateName = selectedCandidate['candidateName']
      }
    },
    handleCloseSkillCertificateViewEvent () {
      this.skillCertificateViewVisible = false
    },
    handleResetSkillCertificateAddEvent () {
      this.skillCertificateAddTitle = '技能证书新增'
    },
    handleCloseSkillCertificateAddEvent (params) {
      if (params && params.search) {
        this.onSearch()
      }
      this.skillCertificateAddVisible = false
    }
  }
}
</script>

<style lang="scss">
</style>